// pages/goods/good-comment.js
import api from "../../utils/api"
import message from '../../utils/message'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        product_id:null,
        page_index:1,
        has_load:false,
        comment_list:[],
        no_data:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        if(options.product_id){

            this.setData({
                product_id: options.product_id
            })

            this.getProductCommentList();
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getProductCommentList:function () {
        var me = this;
        var params = {}
        params.product_id = this.data.product_id;

        var page_index = this.data.page_index;
        if(this.data.has_load){
            page_index++;
        }

        params.page_index = page_index;

        message.show_loading();
        api.product.getProductCommentList( params , function( response ){
            message.hide_loading();
            if( response.status==1 ){


                var no_data = false;

                if(response.data.length<10){
                    no_data = true;
                }

                var data_comment_list = response.data;
                for(var i=0;i<data_comment_list.length ; i++){
                    var str_images =  data_comment_list[i].images;
                    data_comment_list[i].images = JSON.parse( str_images );
                }
                var comment_list = me.data.comment_list;

                comment_list = comment_list.concat( data_comment_list );


                me.setData({
                    comment_list:comment_list,
                    has_load: true,
                    page_index: page_index,
                    no_data: no_data
                })



            }
        })
    },
    showCommentImage:function (e) {
        var obj = e.currentTarget.dataset;

        var index = obj.index;

        var url = obj.m;
        
        var image_list = this.data.comment_list[index].images;

        wx.previewImage({
            current:url,
            urls: image_list
        })
        
    }
})